home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / loop / RCS / loop.c,v < prev   
Encoding:
Text File  |  1992-07-17  |  5.7 KB  |  282 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  srv030:1.2 srv027:1.2 srv026:1.2 srv024:1.2 srv021:1.2 srv018:1.2 srv014:1.2 srv010:1.2 srv008:1.2 srv007:1.2 srv006:1.2 srv004:1.2;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     92.03.12.20.49.02;  author kupfer;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     91.10.18.18.48.37;  author kupfer;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @Simple infinite loop program.
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @Add support for testing signals.
  28. @
  29. text
  30. @/* 
  31.  * loop.c --
  32.  *
  33.  *    Simple program to do something interesting forever.  One use is to
  34.  *    help track down memory leaks in the Sprite server.  Another is to
  35.  *    test signals support.
  36.  *
  37.  * Copyright 1991 Regents of the University of California
  38.  * Permission to use, copy, modify, and distribute this
  39.  * software and its documentation for any purpose and without
  40.  * fee is hereby granted, provided that this copyright
  41.  * notice appears in all copies.  The University of California
  42.  * makes no representations about the suitability of this
  43.  * software for any purpose.  It is provided "as is" without
  44.  * express or implied warranty.
  45.  */
  46.  
  47. #ifndef lint
  48. static char rcsid[] = "$Header: /user5/kupfer/spriteserver/tests/loop/RCS/loop.c,v 1.1 91/10/18 18:48:37 kupfer Exp Locker: kupfer $ SPRITE (Berkeley)";
  49. #endif /* not lint */
  50.  
  51. #include <signal.h>
  52. #include <setjmp.h>
  53. #include <stdio.h>
  54. #include <stdlib.h>
  55. #include <sys/time.h>
  56. #include <sys/types.h>
  57. #include <test.h>
  58. #include <time.h>
  59. #include <unistd.h>
  60.  
  61. /* 
  62.  * If sigpause is used instead of sleep, there are two ways to go.  One is 
  63.  * to set a one-time alarm each time through the loop.  The other is to set 
  64.  * up a periodic timer exactly once.  If this flag is defined, then a 
  65.  * periodic timer is used.
  66.  */
  67. Boolean usePeriodTimer = FALSE;
  68.  
  69. int sleepSecs = 1;        /* number of seconds to pause */
  70. int ignoreSigint = 0;        /* block SIGINT if non-zero */
  71. int sigintFlag = 0;        /* did we get a SIGINT */
  72. int useSigPause = 0;        /* use alarm/sigpause instead of sleep */
  73.  
  74. jmp_buf jmpBuf;            /* setjmp state */
  75.  
  76. #define NO_HANDLER    0
  77. #define SET_FLAG    1    /* handler just sets a flag */
  78. #define DO_LONGJMP    2    /* handler does a longjmp */
  79. int useHandler = NO_HANDLER;    /* register a handler for SIGINT? */
  80.  
  81. void FlagHandler();
  82. void LongjmpHandler();
  83. void CatchAlarm();
  84.  
  85.  
  86. /*
  87.  *----------------------------------------------------------------------
  88.  *
  89.  * main --
  90.  *
  91.  *    In an infinite loop, print the time and sleep a bit.
  92.  *
  93.  * Results:
  94.  *    None.
  95.  *
  96.  * Side effects:
  97.  *    None.
  98.  *
  99.  *----------------------------------------------------------------------
  100.  */
  101.  
  102. int
  103. main(argc, argv)
  104.     int argc;
  105.     char *argv[];
  106. {
  107.     time_t now;
  108.     int argChar;
  109.     extern char *optarg;
  110.     char *usageString = "usage: loop [-i|-h type] [-p [-t]] [-s seconds]\n";
  111.     struct itimerval timer;    /* interval timer */
  112.  
  113.     /* 
  114.      * Parse the command line.
  115.      */
  116.     while ((argChar = getopt(argc, argv, "s:ih:pt")) != EOF) {
  117.     switch (argChar) {
  118.     case 'i':
  119.         ignoreSigint = 1;
  120.         break;
  121.     case 'h':
  122.         switch (*optarg) {
  123.         case 'f':
  124.         useHandler = SET_FLAG;
  125.         break;
  126.         case 'l':
  127.         useHandler = DO_LONGJMP;
  128.         break;
  129.         default:
  130.         Test_PutMessage("handler type is 'f' or 'l'.\n");
  131.         exit(1);
  132.         break;
  133.         }
  134.         break;
  135.     case 'p':
  136.         useSigPause = 1;
  137.         break;
  138.     case 's':
  139.         sleepSecs = atoi(optarg);
  140.         break;
  141.     case 't':
  142.         usePeriodTimer = TRUE;
  143.         break;
  144.     default:
  145.         Test_PutMessage(usageString);
  146.         exit(1);
  147.         break;
  148.     }
  149.     }
  150.     if (ignoreSigint && useHandler != NO_HANDLER) {
  151.     Test_PutMessage(usageString);
  152.     exit(1);
  153.     }
  154.  
  155.     /* 
  156.      * Ignore interrupts if requested.
  157.      */
  158.     if (ignoreSigint) {
  159.     if (signal(SIGINT, SIG_IGN) == BADSIG) {
  160.         perror("can't ignore SIGINT");
  161.         exit(1);
  162.     }
  163.     } else {
  164.     switch (useHandler) {
  165.     case SET_FLAG:
  166.         if (signal(SIGINT, FlagHandler) == BADSIG) {
  167.         perror("can't register FlagHandler");
  168.         exit(1);
  169.         }
  170.         break;
  171.     case DO_LONGJMP:
  172.         if (signal(SIGINT, LongjmpHandler) == BADSIG) {
  173.         perror("can't register LongjmpHandler");
  174.         exit(1);
  175.         }
  176.         break;
  177.     }
  178.     }
  179.  
  180.     if (setjmp(jmpBuf)) {
  181.     Test_PutMessage("\nlongjmp\n");
  182.     }
  183.  
  184.     if (useSigPause) {
  185.     if (signal(SIGALRM, CatchAlarm) == BADSIG) {
  186.         perror("can't register alarm handler");
  187.         exit(1);
  188.     }
  189.     if (usePeriodTimer) {
  190.         timer.it_interval.tv_sec = sleepSecs;
  191.         timer.it_interval.tv_usec = 0;
  192.         timer.it_value = timer.it_interval;
  193.         if (setitimer(ITIMER_REAL, &timer, (struct itimerval *)0) < 0) {
  194.         perror("Can't set interval timer");
  195.         exit(1);
  196.         }
  197.     }
  198.     }
  199.  
  200.     /* 
  201.      * Loop, printing the time every so often.
  202.      */
  203.     if (sleepSecs <= 0) {
  204.     Test_PutMessage("Going into infinite loop.\n");
  205.     }
  206.     for (;;) {
  207.     if (sleepSecs > 0) {
  208.         now = time(0);
  209.         Test_PutTime(now, TRUE);
  210.         if (useSigPause) {
  211.         if (!usePeriodTimer) {
  212.             alarm(sleepSecs);
  213.         }
  214.         (void)sigpause(0);
  215.         } else {
  216.         sleep(sleepSecs);
  217.         }
  218.     }
  219.     if (sigintFlag) {
  220.         Test_PutMessage("\nsigint\n");
  221.         sigintFlag = 0;
  222.     }
  223.     }
  224. }
  225.  
  226. void
  227. FlagHandler(sigNum, code, contextPtr, addr)
  228.     int sigNum, code;
  229.     char *contextPtr, *addr;
  230. {
  231. #ifdef lint
  232.     sigNum = sigNum;
  233.     code = code;
  234.     contextPtr = contextPtr;
  235.     addr = addr;
  236. #endif
  237. #if 0
  238.     printf("\n(%d, %d, 0x%x, 0x%x)\n", sigNum, code, contextPtr, addr);
  239. #endif
  240.     sigintFlag = 1;
  241. }
  242.  
  243. void
  244. LongjmpHandler()
  245. {
  246.     longjmp(jmpBuf, 1);
  247. }
  248.  
  249. void
  250. CatchAlarm()
  251. {
  252. }
  253. @
  254.  
  255.  
  256. 1.1
  257. log
  258. @Initial revision
  259. @
  260. text
  261. @d4 3
  262. a6 2
  263.  *    Simple program to do something interesting forever.  The idea is to 
  264.  *    help track down memory leaks in the Sprite server.
  265. d19 1
  266. a19 1
  267. static char rcsid[] = "$Header: /sprite/lib/forms/RCS/proto.c,v 1.5 91/02/09 13:24:44 ouster Exp $ SPRITE (Berkeley)";
  268. d22 5
  269. d32 24
  270. d74 3
  271. a76 1
  272. main()
  273. d79 71
  274. d151 26
  275. d178 16
  276. a193 3
  277.     now = time(0);
  278.     Test_PutTime(now, TRUE);
  279.     sleep(1);
  280. d195 28
  281. @
  282.